Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
TypeError "cannot set properties of undefined" when Shuffling Array from Module

when I try to shuffle an array I get a TypeError: "cannot set properties of undefined (setting '10')" where 10 is the value of randomNmbrArr[0]. I have 2 other functions that I copied this code from that do work without any problems. I cant find the difference between the working code and the part that doesn't.

let nmr always logs a valid number between the range of 10 and 72. But for some reason randomNmbrArr[nmr] is undefined. Any idea's?

p.s: The code is running from a module but gives the same error in the main file. The array and function are declared before they are called in any way.

working code:

let unicodeArr = [];

for (let i = 0; i < 65537; i++) {
  unicodeArr.push(i);
};

function randomNumber2() {
  let o = 0;
  let nmr2;
  while (++o < 65536) {
    nmr2 = Math.floor(Math.random() * (65536 + 1));
    [unicodeArr[nmr2], unicodeArr[0]] = [unicodeArr[0], unicodeArr[nmr2]];
  };
  return unicodeArr[0];
};

Code that does not work:

let randomNmbrArr = [];

for (let i = 10; i < 100; i++) {
  randomNmbrArr.push(i);
}
console.log(randomNmbrArr)

function makeRandomNmbrs() {
  let k = 0;
  let nmr;
  while (++k < 62) {
    nmr = Math.floor(Math.random() * (62 + 1)) + 10;
    console.log(nmr);
    [randomNmbrArr[nmr], randomNmbrArr[0]] = [randomNmbrArr[0], randomNmbrArr[nmr]]; //TypeError at this line
    console.log(randomNmbrArr)
  }

}

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda